package com.yunhuajinfu.tool;

import android.util.Log;

import com.yunhuajinfu.mian.Config;
import com.yunhuajinfu.response.InvestmentStepOneRes;

import java.math.BigDecimal;

/**
 * Created by ekikousei易皇星 on 16/12/14.
 * E-mail:13764664731@163.com
 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
 * <p/>
 * TODO:类描述:  计算投资收益率
 */
public class YieldUtils {
    public static final String TAG = "YieldUtils";

    public static BigDecimal interest;

    public static String lixi(String tenderAmount, InvestmentStepOneRes res) {
        String string="";
        if (null != res) {
            try {
                if (res.getIsDay().equals("1")) {
                    interest = interestOfDay365(new BigDecimal(tenderAmount), new BigDecimal(res.getAnnualInterestRate()), Integer.parseInt(res.getBorrowTimeLimit()));
                    string = interest.toString().substring(0, interest.toString().indexOf(".") + 3);
                    return string;
                } else {
                    if (res.getRepaymentStyle().equals(Config.REPAYMENT_STYLE_MONTHLY)
                            || res.getRepaymentStyle().equals(Config.REPAYMENT_STYLE_QUARTERLY)) {
                        interest = interestOfMonthlyRepay(new BigDecimal(tenderAmount),
                                new BigDecimal(res.getAnnualInterestRate()),
                                Integer.parseInt(res.getBorrowTimeLimit()));
                    } else {
                        // 一次性还和每月付息到期还本计算、按季付息到期还本
                        interest = interestOfMonth(new BigDecimal(tenderAmount), new BigDecimal(res.getAnnualInterestRate()),
                                Integer.parseInt(res.getBorrowTimeLimit()));
                    }

                     string = interest.toString().substring(0, interest.toString().indexOf(".") + 3);
                    return string;

                }


            } catch (Exception e) {

                Log.i(TAG, "计算利息出错" + e.toString());
                e.printStackTrace();
            }
        }
        return null;

    }

    /**
     * 按天计算利息，一年按照365天算
     *
     * @param totalTenderMoney
     * @param apr
     * @param loanPeriodCount
     * @return
     * @throws Exception
     */
    public static BigDecimal interestOfDay365(BigDecimal totalTenderMoney, BigDecimal apr, int loanPeriodCount) {

        try {
            // 每天的利率
            BigDecimal dlv = apr.divide(new BigDecimal(365), 16, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(100));
            // 每一天的利息
            BigDecimal perDay = totalTenderMoney.multiply(dlv);
            BigDecimal interest = perDay.multiply(new BigDecimal(
                    loanPeriodCount));
            return interest.setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

    }

    /**
     * 按月分期，计算总利息
     *
     * @param tenderMoney     总投资金额
     * @param apr             年利率
     * @param loanPeriodCount 总月数
     * @return
     * @throws Exception
     */
    public static BigDecimal interestOfMonthlyRepay(BigDecimal tenderMoney, BigDecimal apr, Integer loanPeriodCount) {
        try {
            BigDecimal totalMoney = getPaymentMoneyPerMonth(tenderMoney,
                    loanPeriodCount, apr);
            BigDecimal t = totalMoney.multiply(new BigDecimal(loanPeriodCount));
            return t.subtract(tenderMoney);
        } catch (Exception e) {
            e.printStackTrace();

            throw new RuntimeException();
        }
    }


    /**
     * 按月计算利息
     *
     * @param totalTenderMoney 投标总金额
     * @param apr              年利率
     * @return
     * @throws Exception
     */
    public static BigDecimal interestOfMonth(BigDecimal totalTenderMoney,
                                             BigDecimal apr, Integer loanPeriodCount) {
        try {
            BigDecimal dlv = apr.divide(new BigDecimal(12), 6,
                    BigDecimal.ROUND_UP).divide(new BigDecimal(100));

            BigDecimal perMonth = totalTenderMoney.multiply(dlv); // 一个月的利息
            return perMonth.multiply(new BigDecimal(loanPeriodCount));

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 每月分期还款，计算每月还多少
     *
     * @param
     * @param
     * @return
     * @throws Exception
     */
    public static BigDecimal getPaymentMoneyPerMonth(BigDecimal money,
                                                     Integer period, BigDecimal yearRate) {
        try {
            BigDecimal r = new BigDecimal(1);
            // 月利率
            BigDecimal mr = yearRate.divide(new BigDecimal(12), 12,
                    BigDecimal.ROUND_UP).divide(new BigDecimal(100)); // 月利率
            // System.out.println(mr +"  R====月利率");

            BigDecimal rr = r.add(mr); // 1+R
            // System.out.println(rr +"  ====1+R ");

            Double k = Math.pow(rr.doubleValue(), period); // n 次方

            BigDecimal bd = new BigDecimal(k);
            BigDecimal bd2 = bd.setScale(12, BigDecimal.ROUND_HALF_DOWN);

            // System.out.println("N次方"+bd2);
            BigDecimal v = mr.multiply(bd2); // 月利率 X n次方
            // System.out.println("月利率 X n次方" +v );
            BigDecimal muj = bd2.subtract(r); // 被除

            // System.out.println(" n次方  -  1" +muj );
            BigDecimal zhi = v.divide(muj, 12, BigDecimal.ROUND_DOWN);
            // System.out.println(" 相除  -  1" +zhi );

            BigDecimal payMoney = money.multiply(zhi);
            return payMoney.setScale(2, BigDecimal.ROUND_DOWN);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }


}
